<?php
class CustomController extends Controller
{
	/**
	 * @var string the default layout for the views. Defaults to '//layouts/column2', meaning
	 * using two-column layout. See 'protected/views/layouts/column2.php'.
	 */
	public $layout='//layouts/main';

	/**
	 * @return array action filters
	 */
	public function filters()
	{
		return array(
			'accessControl', // perform access control for CRUD operations
		);
	}

	/**
	 * Specifies the access control rules.
	 * This method is used by the 'accessControl' filter.
	 * @return array access control rules
	 */
	public function accessRules()
	{
		return array(
			array('allow',  // allow all users to perform 'index' and 'view' actions
				'actions'=>array('index','view'),
				'users'=>array('*'),
			),
			array('allow', // allow authenticated user to perform 'create' and 'update' actions
				'actions'=>array('create','update','reback','Search','Search2','Reback2','return','success','agentcreate','agentupdate'),
				'users'=>array('@'),
			),
			array('allow', // allow admin user to perform 'admin' and 'delete' actions
				'actions'=>array('admin','delete'),
				'users'=>array('@'),
			),
			array('deny',  // deny all users
				'users'=>array('*'),
			),
		);
	}

	/**
	 * Displays a particular model.
	 * @param integer $id the ID of the model to be displayed
	 */
	public function actionView($id)
	{
		$this->render('view',array(
			'model'=>$this->loadModel($id),
		));
	}

	/**
	 * Creates a new model.
	 * If creation is successful, the browser will be redirected to the 'view' page.
	 */
	public function actionCreate()
	{
		$model=new Custom;

		// Uncomment the following line if AJAX validation is needed
		// $this->performAjaxValidation($model);

		if(isset($_POST['Custom']))
		{
			$model->attributes=$_POST['Custom'];
			$equipment_number=$_POST['Custom']['equipment_number'];
			$equipment=Equipment::model()->find("equipment_number="."'$equipment_number'");
			$equipment_id=$equipment->equipment_id;
			$deposit=$_POST['Custom']['deposit'];
			$isreback=$_POST['isreback'];
			//再次销售
			$userservice=UserService::model()->find("isused=0 AND isreback=1 AND equipment_number ='".$equipment_number."'");
			//循环返回后,更新zy_user_service 使isused=1,access_id=0 
			if($userservice->user_id!=0){
				$userservice->isused=1;
				$userservice->access_id=0;
				$userservice->update();
			}
			//如果是再次销售,先插入userservice
			if($userservice->user_id!=0){
				$rs2 = new UserService();
				$rs2->create_user=yii::app()->user->id;
				$rs2->access_id = $userservice->access_id;
				$rs2->equipment_id=$equipment_id;
				$rs2->equipment_number=$equipment_number;
				$rs2->isreback=1;
				$rs2->isused=0;
				$rs2->service_id = 12;
				$rs2->save();
			}

			if(yii::app()->user->level==1){
				$sale_id=Yii::app()->request->getParam('user_id');
			}else{
				$sale_id=yii::app()->user->id;
			}
			$user_name=$this->setuser();
			$password=rand(10000000, 99999999);
			$telephone=$_POST['Custom']['telephone'];
			$email=$_POST['Custom']['email'];
			$custom_level="8";
			$sql="INSERT INTO zy_user (user_name,password,level,telephone,email) VALUES(:user_name,:password,:level,:telephone,:email)";
			$result = yii::app()->db->createCommand($sql);
			$result->bindParam(":user_name",$user_name);
			$result->bindParam(":password",$password);
			$result->bindParam(":level",$custom_level);
			$result->bindParam(":telephone",$telephone);
			$result->bindParam(":email",$email);
			$result->execute();
			$user=User::model()->find('user_name='.$user_name);
			$model->user_id = $user->user_id;
			$model->password=$password;
			if($model->save())
			$custom=Custom::model()->find('user_id='.$user->user_id);
			$custom_id=$custom->custom_id;
			$package=Package::model()->find('package_id='.$custom->package_id);
			$package_price=$package->package_price;
			$equipment=Equipment::model()->find("equipment_number="."'$custom->equipment_number'");
			$equipment_id=$equipment->equipment_id;
			$equipment_number=$equipment->equipment_number;
			// 根据isreback判断真实客户创建时间.
				if($isreback==0){
					$custom_sql_where=",create_time=now()";
				}
			$custom_sql="update zy_custom set equipment_id=".$equipment_id.",sale_id=".$sale_id.",sale_address='".$equipment->city."'".$custom_sql_where." where user_id =".$custom->user_id;
			$custom_result=yii::app()->db->createCommand($custom_sql);
			$custom_result->execute();
			/** 创建客户时,将该设备的归属人设置为客户.**/
			$equipment_sql="update zy_equipment set  updater='".$custom->user_id."',is_used = 1 where equipment_id =".$equipment_id;
			$equipment_result=yii::app()->db->createCommand($equipment_sql);
			$equipment_result->execute();
			/** 结束 **/
			$telephone_number=$equipment->telephone_number; 
				// 根据isreback判断isused
				if($isreback==2){
					$isused = 0;
				}elseif($isreback==0){
					$isused = 1;
				}
				$user_service_sql="update zy_user_service set deposit='".$deposit."',package_id='".$custom->package_id."', user_id ='".$user->user_id."',package_price = '".$package_price."',equipment_number = '".$equipment_number."',isreback='".$isreback."',isused = '".$isused."'where user_id = 0 and isreback =1 and isused =0 and equipment_id=".$equipment_id;
				$userservice_result=yii::app()->db->createCommand($user_service_sql);
				$userservice_result->execute();
				//获得user_service_id
				$rs=UserService::model()->find("deposit=".$deposit." AND package_id=".$custom->package_id." AND user_id=".$user->user_id." AND package_price=".$package_price." AND equipment_number="."'$equipment_number'"." AND isreback=".$isreback." AND isused=".$isused);		
				$service = Service::model()->find("code="."'phone'");
				$sql2="Insert into zy_user_service (user_id,service_id) values (:user_id,:service_id)";
				$result2 = yii::app()->db->createCommand($sql2);
				$result2->bindParam(":user_id",$model->user_id);
				$result2->bindParam(":service_id",$service->service_id);
				$result2->execute();
		
				//给客户分配a2b帐号
				if($custom->package_id==6){
				//	$package_details_id = mt_rand(1,30);
					$package_details=PackageDetails::model()->find("package_type=:package_type AND isused =:isused",array(":package_type"=>'20',":isused"=>0));
					$package_details_id = $package_details->package_details_id;
				}elseif($custom->package_id==8){
				//	$package_details_id = mt_rand(31,60);
					$package_details=PackageDetails::model()->find("package_type=:package_type AND isused =:isused",array(":package_type"=>'100',":isused"=>0));
					$package_details_id = $package_details->package_details_id;
				}elseif($custom->package_id==7){
				//	$package_details_id = mt_rand(61,90);
					$package_details=PackageDetails::model()->find("package_type=:package_type AND isused =:isused",array(":package_type"=>'50',":isused"=>0));
					$package_details_id = $package_details->package_details_id;
				}
				elseif($custom->package_id==9){
				//	$package_details_id = mt_rand(61,90);
					$package_details=PackageDetails::model()->find("package_type=:package_type AND isused =:isused",array(":package_type"=>'10',":isused"=>0));
					$package_details_id = $package_details->package_details_id;
				}
				$field1=$package_details->name;
				$field2=$package_details->password;
				$field3="billing3.novatel.com.au";	
				if($field1!=""){
				$userservice=UserService::model()->find('user_id=:user_id AND service_id=:service_id',array(':user_id'=>$user->user_id,':service_id'=>$service->service_id));
				$user_service_id=$userservice->user_service_id;
				$loginlist_sql="Insert into zy_login_list (user_service_id,field1,field2,field3) values(:user_service_id,:field1,:field2,:field3)";
				$loginlist_result=yii::app()->db->createCommand($loginlist_sql);
				$loginlist_result->bindParam(":user_service_id",$user_service_id);
				$loginlist_result->bindParam(":field1",$field1);
				$loginlist_result->bindParam(":field2",$field2);
				$loginlist_result->bindParam(":field3",$field3);
				$loginlist_result->execute();
			  $package_details_sql="update zy_package_details set isused = 1 where package_details_id =".$package_details_id;
			  $package_details_result=yii::app()->db->createCommand($package_details_sql);
			  $package_details_result->execute();
//			  if($isreback==0){
//			  	Custom::model()->createsalebonus($custom->package_id,$package_price,$sale_id,$rs->user_service_id);
//			  }
			}
				updateUser_Group($telephone_number,$custom->package_id);
				if($isreback==0){
					$expiredate=date('Y-m-d H:i:s',strtotime("+30 days"));
					setExpireDateByPhoneNumber($telephone_number,$expiredate);
				}
				$this->redirect(array('view','id'=>$model->custom_id));
		}

		$this->render('create',array(
			'model'=>$model,
		));
	}
	public function actionReback()
	{
		$model=new Custom;	
		if(isset($_POST['Custom']))

		{
		
			//设备号,接受人 ,地址,时间
			$equipment_number=$_POST['Custom']['equipment_number'];
			$address=$_POST['Custom']['address'];
			$access_id=$_POST['Custom']['access_id'];
			$time=$_POST['Custom']['time'];
			$custom_id=$_POST['Custom']['custom_id'];
			$user_id=$_POST['Custom']['user_id'];
			if($address=="上海"){
				$custom_sql="update zy_custom set time ='".$time."',access_id = '".$access_id."',address='".$address."',over_time=now() where custom_id="."'$custom_id'";
			}else{
				$custom_sql="update zy_custom set time ='".$time."',access_id = '".$access_id."',address='".$address."',over_time=now() where custom_id="."'$custom_id'";
			}
			
			$custom_result=yii::app()->db->createCommand($custom_sql);
			$custom_result->execute();
			
			$userservice=UserService::model()->find("user_id="."'$user_id' AND isreback=0 AND isused=1");
			$access_id = yii::app()->user->id;
			if($address=="上海"){
				// 设备返回给公司
				$deposit=$userservice->deposit-20;
				$userservice_sql="update zy_user_service set isreback=1,isused=1,deposit='".$deposit."',access_id='".$access_id."' where user_service_id="."'$userservice->user_service_id'";
			}else{
				// 设备返回给销售员
				$deposit=$userservice->deposit;
				$userservice_sql="update zy_user_service set isreback=1,isused=0,deposit='".$deposit."',access_id='".$access_id."' where user_service_id="."'$userservice->user_service_id'";
			}
			
			
			$userservice_result=yii::app()->db->createCommand($userservice_sql);

			$userservice_result->execute();
			if($address=="上海"){
				//设备回公司
				$equipment_sql="update zy_equipment set is_used=0,updater=0,city='' where equipment_number='".$equipment_number."'";
			}else{
				//设备回销售手上.
				$equipment_sql="update zy_equipment set updater=".Yii::app()->user->id." where equipment_number='".$equipment_number."'";
			}
			$equipment__result=yii::app()->db->createCommand($equipment_sql);
			$equipment__result->execute();
			//清空流量.
			$phone_number=Custom::model()->getEquipmentSimCardByUserId($user_id);
			deleteCommonCounter($phone_number);
			/** 禁用账户 **/
			$user_sql="update zy_user set isdelete=1 where user_id=".$user_id;
			$user_result=yii::app()->db->createCommand($user_sql);
			$user_result->execute();
			//清空该用户之前的流量
			$model=$this->loadModel($custom_id);
			$equipment=Equipment::model()->findByPk($userservice->equipment_id);
			deleteCommonCounter($equipment->telephone_number);
			$expiredate=date('Y-m-d H:i:s',strtotime("-24 hours"));
			setExpireDateByPhoneNumber($equipment->telephone_number,$expiredate);
			$this->redirect(array('equipment/viewequipment','user_id'=>yii::app()->user->id));
		}	

		$this->render('reback',array(

				'model'=>$model,

		));
	}
	public function actionReturn()
	{

		$model=new Custom;
		if(isset($_POST['Custom']))
		{
			//设备号,接受人 ,地址,时间
			$address=$_POST['Custom']['address'];
			$time=$_POST['Custom']['time'];
			$custom_id=$_POST['Custom']['custom_id'];
			$telephone=$_POST['Custom']['telephone'];
			$user_id=$_POST['Custom']['user_id'];
			$email=$_POST['Custom']['email'];
			$custom_sql="update zy_custom set time ='".$time."',address='".$address."',telephone='".$telephone."' where custom_id="."'$custom_id'";
			$custom_result=yii::app()->db->createCommand($custom_sql);
			$custom_result->execute();
			$mail  =Yii::app()->mailer;
			$message = $custom_surname.''.$custom_name.'填写异地返回申请表';
			$mail->Host = 'smtp.exmail.qq.com';
			$mail->Port = 25;
			$mail->IsSMTP();
			$mail->SMTPAuth= true;
			$mail->Username = "fanlongtao@maszy.cn";//你的用户名，或者完整邮箱地址
			$mail->Password = "flt198801";//邮箱密码
			$mail->SetFrom('fanlongtao@maszy.cn', 'Jack Van');//发送的邮箱和发送人
			$mail->AddAddress($email);
			$mail->Subject = '异地返回申请表';
			$mail->Body = $message;
			if($mail->send()){
			$this->redirect(array('custom/view','id'=>$custom_id));
			}
		}
		$this->render('return',array(
				'model'=>$model,
		));
	}
	/**
	 * Updates a particular model.
	 * If update is successful, the browser will be redirected to the 'view' page.
	 * @param integer $id the ID of the model to be updated
	 */
	public function actionUpdate($id)
	{
		$model=$this->loadModel($id);
		//$new=$this->loadModel($id);
		// Uncomment the following line if AJAX validation is needed
		// $this->performAjaxValidation($model);

		if(isset($_POST['Custom']))
		{
			$model->attributes=$_POST['Custom'];
			$equipment_number=$_POST['Custom']['equipment_number'];
			$package_id=$_POST['Custom']['package_id'];
			$package=Package::model()->find('package_id='.$package_id);
			$package_price=$package->package_price;
			$sale_id=Custom::model()->getupdatesaleId();
			$isreback=$_POST['isreback'];
			if($isreback==0)
				$and=',isused=1';
				$model->create_time=date('Y-m-d H:i:s');
			if($model->save()){
				$userservice_sql="update zy_user_service set isreback=".$isreback.$and." where user_id = ".$model->user_id." and equipment_number="."'$equipment_number'";
				$userservice_result=yii::app()->db->createCommand($userservice_sql);
				$userservice_result->execute();
				$user_service_id=Custom::model()->getupdateUserServiceId($model->user_id,$equipment_number,$isreback);
				if($isreback==0){
					Custom::model()->createsalebonus($package_id,$package_price,$sale_id,$user_service_id);
					$rs=Equipment::model()->find("equipment_number="."'$equipment_number'");
					$telephone_number=$rs->telephone_number;
					$expiredate=date('Y-m-d H:i:s',strtotime("+30 days"));
					setExpireDateByPhoneNumber($telephone_number,$expiredate);
				}
					$this->redirect(array('admin','isreback'=>$isreback));
				}
		}
		$this->render('update',array(
			'model'=>$model,
		));
	}
	public function actionSuccess()
	{
		$this->render('success');
	}
	/**
	 * Deletes a particular model.
	 * If deletion is successful, the browser will be redirected to the 'admin' page.
	 * @param integer $id the ID of the model to be deleted
	 */
	public function actionDelete($id)
	{
		if(Yii::app()->request->isPostRequest)
		{
			// we only allow deletion via POST request
			$this->loadModel($id)->delete();

			// if AJAX request (triggered by deletion via admin grid view), we should not redirect the browser
			if(!isset($_GET['ajax']))
				$this->redirect(isset($_POST['returnUrl']) ? $_POST['returnUrl'] : array('admin'));
		}
		else
			throw new CHttpException(400,'Invalid request. Please do not repeat this request again.');
	}

	/**
	 * Lists all models.
	 */
	public function actionIndex()
	{
		$dataProvider=new CActiveDataProvider('Custom');
		$this->render('index',array(
			'dataProvider'=>$dataProvider,
		));
	}

	/**
	 * Manages all models.
	 */
	public function actionAdmin()
	{
		$model=new Custom('search');
		$model->unsetAttributes();  // clear any default values
		if(isset($_GET['Custom']))
			$model->attributes=$_GET['Custom'];
		$id=yii::app()->user->id;
		$level=yii::app()->user->level;
		$isreback=yii::app()->request->getParam('isreback');
		if($level==1){
			$sql="SELECT a.custom_id FROM zy_custom a,zy_user_service b WHERE a.user_id=b.user_id AND b.isreback=".$isreback." AND b.service_id=12 ";
		}
		elseif($level==6){
			if($isreback!=""){
			//	$sql="select equipment_number from zy_user_service where isreback=".$isreback." and access_id =".$id;
				if($isreback==0){
					$sql="SELECT a.custom_id FROM zy_custom a,zy_user_service b WHERE a.user_id=b.user_id AND b.isreback=0 AND b.service_id=12 AND a.sale_id=".$id;
				}elseif($isreback==1){
					$sql="SELECT a.custom_id FROM zy_custom a,zy_user_service b WHERE a.user_id=b.user_id AND b.isreback=1 AND b.service_id=12 AND a.sale_id=".$id;
				}elseif($isreback==2){
					$sql="SELECT a.custom_id FROM zy_custom a,zy_user_service b WHERE a.user_id=b.user_id AND b.isreback=2 AND b.service_id=12 AND a.sale_id=".$id;
				}
				
			}else{
			//这句话用来做什么的?
				$sql="select custom_id from zy_custom where sale_id =".$id;
			}
			
		}elseif($level==8){
			$sql="select custom_id from zy_custom where user_id =".$id;
		}else{
			if($isreback!=""){
				$sql="select a.custom_id from zy_custom as a left join zy_user_service b on a.user_id=b.user_id where b.user_id!=0 and b.isreback =".$isreback.' AND b.access_id ='.yii::app()->user->id;
			}else{
				$sql="select custom_id from zy_custom";
			}
		}
		$connection = Yii::app()->db;
		$command = $connection->createCommand($sql);
		$results = $command->queryAll();
		for($i=0;$i<count($results);$i++){
			$result[$i]=$results[$i]['custom_id'];
		}
		$this->render('admin',array(
			'model'=>$model,
			'results'=>$result,
		));
	}
	public function actionSearch()
	{
		$model=new Custom; 
	
		if(isset($_POST['equipment_number']) || isset($_POST['username']))
		{	
			if($_POST['username']==""){
			$equipment_number=$_POST['equipment_number'];
			$custom=Custom::model()->find("equipment_number="."'$equipment_number' AND access_id=0");
			}else{
				$user=User::model()->find('user_name='.$_POST['username']);
				$custom=Custom::model()->find("user_id=".$user->user_id);
			}
			$custom_id=$custom->custom_id;
			if($custom_id!=""){
				$this->redirect(array('custom/reback','custom_id'=>$custom_id));
			}
		}
		$this->render('search',array(
				'model'=>$model,
		));
	}
	// custom返回给agent
	public  function actionSearch2(){
		$model=new Custom;
		
		if(isset($_POST['equipment_number']) || isset($_POST['username']))
		{
			if($_POST['username']==""){
				$equipment_number=$_POST['equipment_number'];
				$custom=Custom::model()->find("equipment_number="."'$equipment_number' AND access_id=0");
			}else{
				$user=User::model()->find('user_name='.$_POST['username']);
				$custom=Custom::model()->find("user_id=".$user->user_id);
			}
			$custom_id=$custom->custom_id;
			if($custom_id!=""){
				$this->redirect(array('custom/reback2','custom_id'=>$custom_id));
			}
		}
		$this->render('search2',array(
				'model'=>$model,
		));
	}
	public function actionReback2(){
		$model=new Custom;
		if(isset($_POST['Custom']))
		
		{
		
			//设备号,接受人 ,地址,时间
			$equipment_number=$_POST['Custom']['equipment_number'];
			$address=$_POST['Custom']['address'];
			$access_id=$_POST['Custom']['access_id'];
			$time=$_POST['Custom']['time'];
			$custom_id=$_POST['Custom']['custom_id'];
			$user_id=$_POST['Custom']['user_id'];
			$id=yii::app()->user->id;
			$custom_sql="update zy_custom set time ='".$time."',access_id = '".$access_id."',address='".$address."',over_time=now() where custom_id="."'$custom_id'";
			$custom_result=yii::app()->db->createCommand($custom_sql);
			$custom_result->execute();
			
			$userservice=UserService::model()->find("user_id="."'$user_id' AND isreback=0 AND isused=1");
			if($address=="上海"){
				// 异地返还
				$deposit=$userservice->deposit-20;
				$userservice_sql="update zy_user_service set isreback=1,isused=1,deposit='".$deposit."' where user_service_id="."'$userservice->user_service_id'";
				//异地返还扣除20个信用额度
				if(yii::app()->user->level==5){
					$agent=AgentPerson::model()->find('user_id='.$id);
				}else{
					$agent=AgentCompany::model()->find('user_id='.$id);
				}
				$agent->credit=$agent->credit-20;
				$agent->update();
			}else{
				// 本地返还
				$deposit=$userservice->deposit;
				$userservice_sql="update zy_user_service set isreback=1,isused=0,deposit='".$deposit."' where user_service_id="."'$userservice->user_service_id'";
			}
			$userservice_result=yii::app()->db->createCommand($userservice_sql);
			$userservice_result->execute();
			
			$equipment_sql="update zy_equipment set updater=".Yii::app()->user->id." where equipment_number='".$equipment_number."'";
			$equipment__result=yii::app()->db->createCommand($equipment_sql);
			$equipment__result->execute();
			
			//清空流量.
			$phone_number=Custom::model()->getEquipmentSimCardByUserId($user_id);
			deleteCommonCounter($phone_number);
			/** 禁用账户 **/
			$user_sql="update zy_user set isdelete=1 where user_id=".$user_id;
			$user_result=yii::app()->db->createCommand($user_sql);
			$user_result->execute();
			//清空该用户之前的流量
			$model=$this->loadModel($custom_id);
			$equipment=Equipment::model()->findByPk($userservice->equipment_id);
			deleteCommonCounter($equipment->telephone_number);
			$expiredate=date('Y-m-d H:i:s',strtotime("-24 hours"));
			setExpireDateByPhoneNumber($equipment->telephone_number,$expiredate);
			$this->redirect(array('equipment/viewagentequipment','user_id'=>yii::app()->user->id));
		}
		
		$this->render('reback2',array(
		
				'model'=>$model,
		
		));
	}
	/**
	 * Returns the data model based on the primary key given in the GET variable.
	 * If the data model is not found, an HTTP exception will be raised.
	 * @param integer the ID of the model to be loaded
	 */
	public function loadModel($id)
	{
		$model=Custom::model()->findByPk($id);
		if($model===null)
			throw new CHttpException(404,'The requested page does not exist.');
		return $model;
	}

	/**
	 * Performs the AJAX validation.
	 * @param CModel the model to be validated
	 */
	protected function performAjaxValidation($model)
	{
		if(isset($_POST['ajax']) && $_POST['ajax']==='custom-form')
		{
			echo CActiveForm::validate($model);
			Yii::app()->end();
		}
	}
	public  function setuser()
	{	
		$user_name=rand(10000000, 99999999);
		$user_result=User::model()->find('user_name='.$user_name);
		if($user_result!=""){
			setuser();
		}
		return $user_name;
	}
	//代理创建custom的action
	public function actionAgentCreate(){
		$model=new Custom;

		// Uncomment the following line if AJAX validation is needed
		// $this->performAjaxValidation($model);

		if(isset($_POST['Custom']))
		{
			$model->attributes=$_POST['Custom'];
			$deposit=$_POST['Custom']['deposit'];
			$isreback=$_POST['isreback'];
			$password=rand(10000000, 99999999);
			$equipment_number=$_POST['Custom']['equipment_number'];
			// 设备循环使用时
			Custom::model()->reinsertuserservice($equipment_number);
			$user_name=Custom::model()->CreateUser($password);
			$user=User::model()->find('user_name='.$user_name);
			$model->user_id = $user->user_id;
			$model->password=$user->password;
			if($model->save())
			$custom=Custom::model()->getCustom($user->user_id);
			$package=Custom::model()->getPackage($custom->package_id);
			$equipment=Custom::model()->getEquipment($custom->equipment_number);
			$custom_result=Custom::model()->updateCustom($isreback,$user->user_id,$equipment->equipment_id,$equipment->city);
			$equipment_result=Custom::model()->updateEquipment($equipment->equipment_id,$user->user_id);
			$telephone_number=$equipment->telephone_number; 
				$userservice_result=Custom::model()->updateUserService($isreback,$deposit,$package->package_id,$model->user_id,$package->package_price,$equipment->equipment_id,$equipment->equipment_number);
				$user_service_id=$userservice_result->user_service_id;
				$agentperson=Custom::model()->getAgentNumber();
				$agent_number=$agentperson->agent_number;
				$company_number=$agentperson->company_number;
				Custom::model()->CreateLoginList($custom->package_id);
				Custom::model()->updatecredit($package->package_price,$isreback);
				if(yii::app()->user->level==4 || yii::app()->user->level==7){
					Custom::model()->createbonus($isreback,$package->package_id,$package->package_price,$user_service_id,$agent_number,$company_number);
				}
				updateUser_Group($telephone_number,$custom->package_id);
				if($isreback==0){
					$expiredate=date('Y-m-d H:i:s',strtotime("+30 days"));
					setExpireDateByPhoneNumber($telephone_number,$expiredate);
				}
				$this->redirect(array('view','id'=>$model->custom_id));
			
		}

		$this->render('agentcreate',array(
			'model'=>$model,
		));
	}
	public function actionagentupdate($id){
		$model=$this->loadModel($id);
		if(isset($_POST['Custom']))
		{
			$model->attributes=$_POST['Custom'];
			$equipment_number=$_POST['Custom']['equipment_number'];
			$package_id=$_POST['Custom']['package_id'];
			$package=Package::model()->find('package_id='.$package_id);
			$isreback=$_POST['isreback'];
			if($isreback==0)
				$and=',isused=1';
			$model->create_time=date('Y-m-d H:i:s');
			if($model->save()){
				$userservice_sql="update zy_user_service set isreback=".$isreback.$and." where user_id = ".$model->user_id." and equipment_number="."'$equipment_number'";
				$userservice_result=yii::app()->db->createCommand($userservice_sql);
				$userservice_result->execute();
				$userservice=UserService::model()->find("isreback=".$isreback." AND user_id=".$model->user_id." AND equipment_number="."'$equipment_number'");
				$user_service_id=$userservice->user_service_id;
				$package=Package::model()->find('package_id='.$package_id);
				$agentperson=Custom::model()->getAgentNumber();
				$agent_number=$agentperson->agent_number;
				$company_number=$agentperson->company_number;
				if(yii::app()->user->level==4 || yii::app()->user->level==7){
					Custom::model()->createbonus($isreback,$package->package_id,$package->package_price,$user_service_id,$agent_number,$company_number);
					Custom::model()->updatecredit($package->package_price,$isreback);
				}
				if($isreback==0){
					$rs=Equipment::model()->find("equipment_number="."'$equipment_number'");
					$telephone_number=$rs->telephone_number;
					$expiredate=date('Y-m-d H:i:s',strtotime("+30 days"));
					setExpireDateByPhoneNumber($telephone_number,$expiredate);
				}
				$this->redirect(array('admin','isreback'=>$isreback));
			}
		}
		$this->render('agentupdate',array(
				'model'=>$model,
		));
	}
}
